<?php
namespace app\admin\controller;
use app\admin\logic\ConfingsLogin;
use think\Request;

Class Confings extends BaseAdmin
{

    public function listing()
    {
        return $this->fetch();
    }

    public function ajax()
    {
        $limit = $this->_get('limit', 10);
        $r = (new ConfingsLogin())->listing($limit);
        return $r;
    }


    public function create(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $logic = new ConfingsLogin();
            if ($logic->create($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            return $this->fetch();
        }
    }


    public function update(Request $request)
    {
        $logic = new ConfingsLogin();
        if ($request->isPost()) {
            $data = $request->only(['name','content','value','id']);
            $data['update_time'] = time();
            if ($logic->update($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            $id = $this->_get('id');
            $r = $logic->update($id, 'get');
            $this->assign('info', $r);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        $r = (new ConfingsLogin())->delete($id);
        return apiReturnSuc($r);
    }

}